#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QMessageBox>
#include <QProcess>
#include <QFileDialog>
#include <QTextCodec>


QString openFileName;
QString item_str[100000];
QString word_str[100000];
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
    connect(ui->a_open, SIGNAL(triggered()), this, SLOT(on_OpenFile_clicked()));
    connect(ui->a_about, SIGNAL(triggered()), this, SLOT(on_About_clicked()));
//    ui->tableWidget->setRowCount(10);
//    ui->tableWidget->setColumnCount(5);
//    ui->tableWidget->setItem(3,2,new QTableWidgetItem("word_str[item_num-T-1])"));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_OpenFile_clicked()
{
    openFileName = QFileDialog::getOpenFileName(this,tr(""),"",tr("SUPPORT(*.asset *.dat);;ASSET(*.asset);;DAT(*.dat);;all files(*.*)")); //选择路径
    setWindowTitle("LanguageAssetEditor-"+openFileName);
    QFile aFile(openFileName);  //以文件方式读出
    if (!(aFile.open(QIODevice::ReadOnly))){
        QMessageBox::warning(NULL,"错误","无法打开文件！");
        return;
    }
    QDataStream aStream(&aFile);
   char rraw[400000];
   int T,item_num,b_num,z_num;

   //read items
   aStream.readRawData(rraw, 40);
   qDebug()<<"====="<<rraw<<endl;
   aStream.readRawData(rraw, 4);
   item_num=(int)((uchar)rraw[0]|(uchar)rraw[1]<<8|(uchar)rraw[2]<<16|(uchar)rraw[3]<<24);
   qDebug()<<hex<<item_num<<endl;
    T=item_num;
    while(T--){
        memset(rraw,0,sizeof(rraw));

        aStream.readRawData(rraw, 4);
        b_num=(int)((uchar)rraw[0]|(uchar)rraw[1]<<8|(uchar)rraw[2]<<16|(uchar)rraw[3]<<24);
        aStream.readRawData(rraw, b_num);
        qDebug()<<item_num-T<<":"<<rraw<<endl;
        item_str[item_num-T-1]=QString(QLatin1String(rraw));
        z_num=4-b_num%4;
        if(z_num==4)z_num=0;
        aStream.readRawData(rraw, z_num);

    }

    //read words
    aStream.readRawData(rraw, 4);
    item_num=(int)((uchar)rraw[0]|(uchar)rraw[1]<<8|(uchar)rraw[2]<<16|(uchar)rraw[3]<<24);
    qDebug()<<hex<<item_num<<endl;
     T=item_num;
     while(T--){
         memset(rraw,0,sizeof(rraw));
         aStream.readRawData(rraw, 4);
         b_num=(int)((uchar)rraw[0]|(uchar)rraw[1]<<8|(uchar)rraw[2]<<16|(uchar)rraw[3]<<24);
         aStream.readRawData(rraw, b_num);
         //qDebug()<<item_num-T<<":";//<<rraw<<endl;
         word_str[item_num-T-1]=QString(QString::fromLocal8Bit(rraw));
         z_num=4-b_num%4;
         if(z_num==4)z_num=0;
         aStream.readRawData(rraw, z_num);
     }

//   int i = rraw[0];
//   QString strl = QString::number(i, 16);
//        qDebug()<<strl;

     ui->tableWidget->setRowCount(item_num);
     ui->tableWidget->setColumnCount(2);
     T=item_num;
     while(T--){
         //ui->tableWidget->setItem(item_num-T,0,new QTableWidgetItem(QString::number(item_num-T-1)));
         ui->tableWidget->setItem(item_num-T-1,0,new QTableWidgetItem(item_str[item_num-T-1]));
         ui->tableWidget->setItem(item_num-T-1,1,new QTableWidgetItem(word_str[item_num-T-1]));
     }
     ui->tableWidget->show();
}

 void MainWindow::on_About_clicked(){
     AboutDialog dialog;
     dialog.exec();
     //about_view = new PCLViewer(this) ;//将类指针实例化
     //about_view->show();
 }

void MainWindow::on_pushButton_4_clicked()
{
    QString csvFileName;
    if(item_str[0]==""){
        QMessageBox::warning(NULL,"提示","无导出内容！");
    }else{
        csvFileName = QFileDialog::getSaveFileName(this,"Save File","","TXTFile(*.txt)"); //选择路径
        if(csvFileName == "")
            {
                return;
            }
            QFile file(csvFileName);
            if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
            {
                QMessageBox::warning(this,"error","open file failure!");
                return;
            }
            else
            {
                QTextStream textStream(&file);
                int i=0;
                while(item_str[i] != ""){
                    textStream<<item_str[i]<<"\t"<<word_str[i].replace("\n","\\n")<<endl;
                    i++;
                }
                QMessageBox::warning(this,"tip","导出完成");
                file.close();
            }
    }
}
